
from PyQt5.QtWidgets import (QApplication,QWidget,QMessageBox)

import sys,os
from forms.component import 打印机
import win32api,win32print
# from setting import app_dist
import json
import PyPDF2
import win32com.client as client
import time,threading
from forms.config import basePath,app_info,app_dist
class 打印机_基类_窗体(QWidget):
    def __init__(self,type):
        super(打印机_基类_窗体, self).__init__()
        self.type=type

        self.initUi()
        self.initType()
        self.ui.checkBox.setChecked(True) #设置默认打印机
    def initType(self):
        if self.type=='打印质检报告':
            self.ui.label_3.setVisible(False) #标签报表
            self.ui.comboBox_2.setVisible(False)
            self.ui.label_4.setVisible(False) #标签打印机
            self.ui.comboBox_3.setVisible(False)
            self.ui.label.setVisible(False) #质量标准
            self.ui.comboBox.setVisible(False)
        elif self.type=='打印质量标准':
            self.ui.label_3.setVisible(False) #标签报表
            self.ui.comboBox_2.setVisible(False)
            self.ui.label_4.setVisible(False) #标签打印机
            self.ui.comboBox_3.setVisible(False)
            self.ui.label_5.setVisible(False) #质检报告
            self.ui.comboBox_4.setVisible(False)
        elif self.type=='打印标签':

            self.ui.label_5.setVisible(False) #质检报告
            self.ui.comboBox_4.setVisible(False)
            self.ui.label.setVisible(False)  # 质量标准
            self.ui.comboBox.setVisible(False)
    def initUi(self):
        '''窗口的初始化'''
        self.ui = 打印机.Ui_Form()
        self.ui.setupUi(self)
        self.ui.pushButton.clicked.connect(self.print_OK)
        prints = win32print.EnumPrinters(6)
        pr = [prints[i][-2] for i in range(len(prints))]
        self.ui.comboBox.addItems(pr)
        self.ui.comboBox_3.addItems(pr)
        self.ui.comboBox_4.addItems(pr)
        # 显示所有报表样式
        filename_List, path = self.getReportStyles()  # 获取所有样式
        self.ui.comboBox_2.addItems(filename_List)

        默认样式 = self.getReportDefaultStyle()  # 获取默认样式
        self.ui.comboBox_2.setCurrentText(默认样式)
        质量标准默认打印机,质检报告默认打印机,标签默认打印机=self.getReportDefaultPrinters() #获取默认打印机
        self.ui.comboBox_3.setCurrentText(标签默认打印机)
        self.ui.comboBox.setCurrentText(质量标准默认打印机)
        self.ui.comboBox_4.setCurrentText(质检报告默认打印机)



    def print_OK(self):
        # 打印

        QMessageBox.information(self, '提示', '打印比较慢，请耐心等待，不要多点了')

        默认样式 = app_dist.get('defaultReportStyles')
        file_dir = '资料打印'


        默认样式 = 默认样式.get(file_dir)

        if self.ui.checkBox.isChecked():
           if self.ui.comboBox_3.isVisible():
                默认样式['标签默认打印机']=self.ui.comboBox_3.currentText()
           if self.ui.comboBox.isVisible():
               默认样式['质量标准默认打印机'] = self.ui.comboBox.currentText()
           if self.ui.comboBox_4.isVisible():
               默认样式['质检报告默认打印机'] = self.ui.comboBox_4.currentText()

        with open(os.path.join(basePath,'my.json'),'w',encoding='utf8') as fp:
            json.dump(app_dist,fp,ensure_ascii=False)
    def getReportStyles(self):
        '''获取所有报表样式'''
        path = os.path.join(basePath, 'report', '资料打印', '总报表')
        # 先判断是否为一个文件夹，然后在判断里面有多少文件
        filename_List = []
        if os.path.isdir(path):
            for pat in os.listdir(path):
                if os.path.isfile(os.path.join(path , pat)):
                    fileName = os.path.splitext(pat)
                    if fileName[1] == '.grf':
                        filename_List.append(fileName[0])
        return filename_List, path
    def getReportDefaultStyle(self):
        file_dir = '资料打印'
        默认样式 = app_dist.get('defaultReportStyles')

        默认样式 = 默认样式.get(file_dir).get('总报表')
        return 默认样式

    def getReportDefaultPrinters(self):
        file_dir = '资料打印'
        默认样式 = app_dist.get('defaultReportStyles')

        默认样式 = 默认样式.get(file_dir)

        return 默认样式.get('质量标准默认打印机'),默认样式.get('质检报告默认打印机'),默认样式.get('标签默认打印机')
class 打印机_简单基类_窗体(QWidget):
    def __init__(self,defaultStyle=None,fileNameList=None,fileNameBasePath=None,app=None):
        super().__init__()
        self.fileNameList=fileNameList #报表样式[]
        self.defaultStyle=defaultStyle #默认样式
        self.fileNameBasePath=fileNameBasePath #基本路径
        self.app=app
        self.initUi()
        self.ui.checkBox.setChecked(True)
        self.ui.checkBox_2.setChecked(True)
    def initUi(self):
        '''窗口的初始化'''
        self.ui = 打印机.Ui_Form()
        self.ui.setupUi(self)
        self.ui.pushButton.clicked.connect(self.print_OK)
        #获取打印机
        prints = win32print.EnumPrinters(6)
        pr = [prints[i][-2] for i in range(len(prints))]
        self.ui.comboBox.addItems(pr)
        #获取要打印的报表
        self.getReportList()
        self.ui.comboBox_2.currentTextChanged.connect(self.getDefaultPrinter)
        #根据样式获取默认默认打印机和打印页数
        self.getDefaultPrinter()


    def getDefaultPrinter(self):
        '''获取默认打印机和打印页数'''
        路径_list = str(os.path.split(self.fileNameBasePath)[0]).split('\\')
        root_i = 0
        for i in range(len(路径_list)):
            if 路径_list[i] == 'report':
                root_i = i
                break

        file_dir = 路径_list[root_i + 1:-1]
        fileName = self.ui.comboBox_2.currentText()
        # print(file_dir)
        默认样式 = self.app.app_dist.get('报表默认样式')
        for fi in file_dir:
            默认样式 = 默认样式.get(fi)
            # print(fi,默认样式)
        默认样式 = 默认样式.get('报表设置')
        if 默认样式:
            默认样式 = 默认样式.get(fileName)
            if 默认样式:
                默认打印机 = 默认样式.get('默认打印机')
                默认样式 = 默认样式.get('打印页数')
                if 默认样式:
                    self.ui.lineEdit.setText(默认样式)
                if 默认打印机:
                    self.ui.comboBox.setCurrentText(默认打印机)
    def getReportList(self):
        for fileNamePath in self.fileNameList:
            fileNameFullPath=os.path.join(self.fileNameBasePath,fileNamePath+'.grf')
            print(fileNameFullPath)
            if os.path.isfile(fileNameFullPath):
                self.ui.comboBox_2.addItem(fileNamePath)
        if self.defaultStyle:
            self.ui.comboBox_2.setCurrentText(self.defaultStyle)
    def print_OK(self):
        # 把默认打印机和默认打印页数写进info.json 文件里
        路径_list = str(os.path.split(self.fileNameBasePath)[0]).split('\\')
        root_i = 0
        for i in range(len(路径_list)):
            if 路径_list[i] == 'report':
                root_i = i
                break

        file_dir = 路径_list[root_i + 1:-1]

        # print(file_dir)
        默认样式 = self.app.app_dist.get('报表默认样式')
        # print(默认样式, file_dir)
        for fi in file_dir:
            默认样式 = 默认样式.get(fi)

        if self.ui.checkBox.isChecked():

            默认样式.get('报表设置')[self.ui.comboBox_2.currentText()] = {}
            默认样式.get('报表设置')[self.ui.comboBox_2.currentText()]['默认打印机'] = self.ui.comboBox.currentText()
        if self.ui.checkBox_2.isChecked():
            if self.ui.lineEdit.text():
                打印页数 = self.ui.lineEdit.text()
                页数 = ''
                for 页 in 打印页数.split('-'):
                    try:
                        页数 += str(int(页)) + '-'

                    except:
                        QMessageBox.information(self, '提示', '打印保存失败，打印页数异常')
                        return

                默认样式.get('报表设置')[self.ui.comboBox_2.currentText()]['打印页数'] = 页数[:-1]
            else:
                默认样式.get('报表设置')[self.ui.comboBox_2.currentText()]['打印页数'] = ''
        with open(os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'setting/info.json'),
                  'w', encoding='utf8') as fp:
            json.dump(self.app.app_dist, fp, ensure_ascii=False)

if __name__=='__main__':
    app = QApplication(sys.argv)
    登录_ = 打印机_基类_窗体(path=r'D:\天翼云盘下载\GMP-home\GMP1\report\生产管理\食品\生产车间\通版生产\子报表\生产记录\11.docx',
                    path2=r'D:\天翼云盘下载\GMP-home\GMP1\temp\11.docx')
    登录_.show()

    sys.exit(app.exec_())
